package com.cloudbroker.bcs.common.httpproxy.base;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudbroker.bcs.common.constants.BasicFields;
import com.cloudbroker.bcs.common.context.BizDataContext;
import com.cloudbroker.bcs.common.context.DataContext;
import com.cloudbroker.bcs.common.util.HSBCSHttpClient;
import com.cloudbroker.bcs.common.vo.HSBCSVOBase;

public class HttpProxyBase {

    private static final Logger LOG = LoggerFactory.getLogger(HttpProxyBase.class);

    protected HSBCSHttpClient httpClient;
    
    protected Map<String, Object> callRemote(String function, HSBCSVOBase param) {
        // 如果线程中有access_token，写到HSBCSVO中
        String access_token = BizDataContext.getAccess_token();
        if (null != access_token) {
            param.setUser_token(access_token);
        }
        // 如果线程中有tenantId，写到HSBCSVO中
        String tenantId = DataContext.getTenantId();
        if (null != tenantId) {
            param.setTenant_id(tenantId);
        }
        return httpClient.postFuncJSONFetchJSON(function, param);
    }
    
    protected Map<String, Object> callRemote(String function, Map<String, Object> param, String charset) {
        // 如果线程中有access_token，写到HSBCSVO中
        String access_token = BizDataContext.getAccess_token();
        if (null != access_token) {
            param.put(BasicFields.ACCESS_TOKEN, access_token);
        }
        // 如果线程中有tenantId，写到HSBCSVO中
        String tenantId = DataContext.getTenantId();
        if (null != tenantId) {
            param.put(BasicFields.TENANT_ID, tenantId);
        }
        return httpClient.postFuncFormFetchJSON(function, param, charset);
    }
    
    protected Map<String, Object> callRemote(String function, Map<String, Object> param) {
        return callRemote(function, param, null);
    }

    /**
     * 新增转换的调用远程方法，
     * @param function
     * @param param
     * @param <V>
     * @return
     * @throws ClassCastException
     */
    protected <V> Map<String,V> callRemoteByConvert(String function, Map<String, ?> param) throws  ClassCastException{
        Map<String,Object> map = callRemote(function, Collections.unmodifiableMap(param), null);
        Map<String,V> newMap =new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            newMap.put(entry.getKey(), (V) entry.getValue());
        }
        return  newMap;
    }

    protected Map<String, Object> callRemoteRequestJSON(String function, Object param) {
        return httpClient.postFuncJSONFetchJSON(function, param);
    }
    
    protected Map<String, Object> callRemoteRequestForm(String function, Map<String, Object> param, String charset) {
        return httpClient.postFuncFormFetchJSON(function, param, charset);
    }
    
    protected Map<String, Object> callRemoteRequestForm(String function, Map<String, Object> param) {
        return httpClient.postFuncFormFetchJSON(function, param, null);
    }
    
    protected Map<String, Object> callRemoteRequestJSON(String function) {
        return httpClient.postFuncJSONFetchJSON(function);
    }
    
    protected Map<String, Object> callRemoteRequestForm(String function) {
        return httpClient.postFuncFormFetchJSON(function);
    }
    
    public HSBCSHttpClient getHttpClient() {
        return httpClient;
    }
    
    public void setHttpClient(HSBCSHttpClient httpClient) {
        this.httpClient = httpClient;
    }
    
}
